package com.zzh.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zzh.dao.UserMapper;
import com.zzh.domain.*;
import com.zzh.service.ResourceService;
import com.zzh.service.UserService;
import com.zzh.utils.MD5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public PageInfo<User> findAllUserByPage(UserVo userVo) {
        PageHelper.startPage(userVo.getCurrentPage(), userVo.getPageSize());
        List<User> allUser = userMapper.findAllUserByPage(userVo);
        PageInfo<User> pageInfo = new PageInfo<>(allUser);

        System.out.println("总条数："+pageInfo.getTotal());
        System.out.println("总页数："+pageInfo.getPages());
        System.out.println("当前页："+pageInfo.getPageNum());
        System.out.println("每页显示长度："+pageInfo.getPageSize());
        System.out.println("是否第一页："+pageInfo.isIsFirstPage());
        System.out.println("是否最后一页："+pageInfo.isIsLastPage());

        return pageInfo;
    }

    @Override
    public void updateUserStatus(int id, String status) {
        userMapper.updateUserStatus(id, status);
    }

    @Override
    public User login(User user) {
        User userMD5 = userMapper.login(user);
        if (userMD5 != null && MD5.verify(user.getPassword(), "vivian", userMD5.getPassword())) {
            return userMD5;
        }else {
            return null;
        }
    }

    @Override
    public List<Role> findUserRoleById(int id) {
        return userMapper.findUserRoleById(id);
    }

    @Override
    public void userContextRole(UserVo userVo) {
        userMapper.deleteUserContextRole(userVo.getUserId());
        for (Integer roleId : userVo.getRoleIdList()) {
            User_Role_relation user_role_relation = new User_Role_relation();
            user_role_relation.setUserId(userVo.getUserId());
            user_role_relation.setRoleId(roleId);
            Date date = new Date();
            user_role_relation.setCreatedTime(date);
            user_role_relation.setUpdatedTime(date);
            user_role_relation.setCreatedBy("system");
            user_role_relation.setUpdatedby("system");
            userMapper.userContextRole(user_role_relation);
        }
    }

    @Override
    public ResponseResult getUserPermissions(Integer id) {
        //获取当前用户拥有的权限
        List<Role> roles = userMapper.findUserRelationRoleById(id);
        //获取角色id，保存到list
        List<Integer> list = new ArrayList<>();
        for (Role role : roles) {
            list.add(role.getId());
        }
        //根据角色id查询父菜单
        List<Menu> menus = userMapper.findParentMenuByRoleId(list);
        //封装父菜单下的子菜单
        for (Menu menu : menus) {
            List<Menu> subMenu = userMapper.findSubMenuByPid(menu.getId());
            menu.setSubMenuList(subMenu);
        }
        //获取资源权限
        List<Resource> resources = userMapper.findResourceByRoleId(list);
        //封装数据
        Map<String, Object> map = new HashMap<>();
        map.put("menuList", menus);
        map.put("resourceList", resources);
        ResponseResult result = new ResponseResult(true, 200, "响应成功", map);
        return result;
    }
}
